package org.zjt.spiextensiondemo.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.zjt.spiextensiondemo.collector.TraceCollector;
import org.zjt.spiextensiondemo.util.SPIExtensionLoader;


/**
 * @author <a href="juntao.zhang@bkjk.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.spiextensiondemo.config
 * @date 2017/12/4 18:06
 * @see
 */
@Configuration
@EnableConfigurationProperties(SPIProperties.class)
public class SPIConfig {

    @Bean
    @ConditionalOnProperty(prefix = "spi.traceCollector", name = "enabled", matchIfMissing = true)
    public TraceCollector traceCollector(SPIProperties spiProperties)  {
        SPIExtensionLoader<TraceCollector> extensionLoader = SPIExtensionLoader.getExtensionLoader(TraceCollector.class);
        if (StringUtils.hasText(spiProperties.getTraceCollector())){
            return extensionLoader.getExtension(spiProperties.getTraceCollector());
        }
        return extensionLoader.getAdaptiveExtension();
    }


    @Bean
    @ConditionalOnProperty(prefix = "spi.traceCollector", name = "enabled", matchIfMissing = true)
    public String msg(TraceCollector traceCollector) {
        System.err.println("Trace收集器名称: "+traceCollector);
        return "helloWorld";
    }

}
